/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package himilco.projects.ocana;

import com.himilco.ocana.workflow.core.dao.shared.entities.Incident;
import com.himilco.ocana.workflow.core.dao.shared.entities.Ticket;
import java.util.Date;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.EJB;
import javax.ejb.MessageDriven;
import javax.jms.*;

/**
 *
 * @author oussema
 */
@MessageDriven(mappedName = "jms/Queue", activationConfig = {
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
public class IncidentMDB implements MessageListener {

    @EJB
    IncidentListener incidentListener;

    public IncidentMDB() {
    }

    @Override
    public void onMessage(Message message) {
        System.out.println("#New Message Recieved");
        ObjectMessage msg = null;
        try {
            if (message instanceof TextMessage) {
                System.out.println("##TextMessage Recieved at " + new Date());
                TextMessage msgTXT = (TextMessage) message;
                System.out.println(message.getStringProperty("incident_subject"));
                System.out.println(message.getStringProperty("incident_degree"));
                Ticket ticket1 = new Ticket();
                Incident incident1 = new Incident("CASH PROBLEM", message.getStringProperty("incident_subject"), message.getStringProperty("incident_degree"), "IMPORTANT", "1H", "IMPORTANT", "IMPORTANT", "IMPORTANT", "IMPORTANT", "IMPORTANT", "IMPORTANT", "IMPORTANT", "IMPORTANT", "IMPORTANT");
                ticket1.setStartTime(new Date());
                ticket1.setStatus("OPEN");
                ticket1.setIncident(incident1);
                incidentListener.saveTicket(ticket1, incident1);
            } else {
                System.out.println("###No valid message for this Queue MDB");
            }
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}
